﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using GroupManager.Class;
namespace GroupManager
{
    public partial class ThemHoatDong : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                for (int i = 1; i < 13; i++)
                {
                    drlThang.Items.Add(i.ToString());
                }
                int nam = DateTime.Now.Year;
                for (int i = nam - 1; i < nam + 2; i++)
                {
                    drlNam.Items.Add(i.ToString());
                }
            }
        }

        protected void btLuuNhap_Click(object sender, EventArgs e)
        {
            string tenHoatDong = txtTenHoatDong.Text.Trim();
           
            if (string.IsNullOrEmpty(tenHoatDong))
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "alert", "alert('Bạn phải nhập tên hoạt động!');", true);
                return;
            }
            string noiDung = txtNoiDung.Text.Trim();
            string path;
            if (FileUpload1.HasFile)
            {
                string loaifile = Path.GetExtension(FileUpload1.FileName);
                path = @"~/Resource/images/image_hoatdong/" + LopHoatDong.MaAnh().ToString() + loaifile;
                FileUpload1.SaveAs(HttpContext.Current.Server.MapPath("" + path));
            }
            else
            {
                path = @"~/Resource/images/image_hoatdong/default.png";
            }
            string mdoinhom = Request.QueryString["doinhom"];
            int madoinhom = int.Parse(mdoinhom);
            LopHoatDong.ThemHoatDong(tenHoatDong, noiDung, madoinhom, int.Parse(drlThang.Text), int.Parse(drlNam.Text), false, path);
            btHienThi.Enabled = false;
            btLuuNhap.Enabled = false;
        }

        protected void btHienThi_Click(object sender, EventArgs e)
        {
            string tenHoatDong = txtTenHoatDong.Text.Trim();

            if (string.IsNullOrEmpty(tenHoatDong))
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "alert", "alert('Bạn phải nhập tên hoạt động!');", true);
                return;
            }
            string noiDung = txtNoiDung.Text.Trim();
            string path;
            if (FileUpload1.HasFile)
            {
                string loaifile = Path.GetExtension(FileUpload1.FileName);
                path = @"~/Resource/images/image_hoatdong/" + LopHoatDong.MaAnh().ToString() + loaifile;
                FileUpload1.SaveAs(HttpContext.Current.Server.MapPath("" + path));
            }
            else
            {
                path = @"~/Resource/images/image_hoatdong/default.png";
            }
            string mdoinhom = Request.QueryString["doinhom"];
            int madoinhom = int.Parse(mdoinhom);
            LopHoatDong.ThemHoatDong(tenHoatDong, noiDung, madoinhom, int.Parse(drlThang.Text), int.Parse(drlNam.Text), true, path);
            btHienThi.Enabled = false;
            btLuuNhap.Enabled = false;
        }
    }
}